public class Solution {
    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode second1 = new ListNode();
        ListNode second = second1;
        second.next = head;
        ListNode frist = new ListNode();
        frist = head;
        //让frist领先second n 个节点
        while (n > 0) {
            frist = frist.next;
            n--;
        }

        //让second和frist一起走
        while (frist != null) {
            frist = frist.next;
            second = second.next;
        }
        //second走到倒数第n个节点的前一个
        second.next = second.next.next;
        return second1.next;
    }
}
